# 记录并处理无数据区

import os
import time
from shape_judgment.write_to_file import write_line_to_file
import pyautogui
import logging

# # 配置日志 只能配置一次, 另一次会被覆盖
# logging.basicConfig(
#     level=logging.INFO,
#     format='%(asctime)s - %(message)s',
#     handlers=[
#         logging.FileHandler("no_data_area.log"),
#         logging.StreamHandler()  # 同时输出到控制台
#     ]
# )

folder_path = 'data_from_east_money_desktop_app'
file_name = 'no_data area.txt'

class NoDataArea:
    def __init__(self):
        self.state_none_data_area = 0 # 0: 有数据的区域, 1: 记录了无数据区的开始,等待无数据区 0: 记录了无数据区的结束 # 开始与结束的股票ID都是有数据的. 这就需要把pagedowm放到分支中.
        self.stockid_none_data_area_start=""
        self.stockid_none_data_area_end="" # 区域的头尾, 这两个值都是有数据的. 这就需要把pagedowm放到分支中.
        # 设置一些内置的无数据区 需在前面加上`符号, 因为之前的股票ID都加了, 没这个符号, 字符串比对就过不去
        self.list_no_data_area = [
            ("`003816","200011"),
            ("`302132","600000"),
            ("`688981","920000"), # 下面的九号公司-WD由于名称过长, 会没能直接取到股票ID,所以放弃这一只股票为好.
            ("`689009","920000"),
            ("`920992","完")] # 
        self.read_file()
        # 当从无数据区出来时, 记录头尾, stockid_none_data_area_
        # 试打开文件, 提前触发错误:
        file_path = os.path.join(folder_path, file_name)
        file = open(file_path, 'a')  # 试文件能否打开, 提前知道崩溃的情况
        file.close()                # 关闭文件

    # 记录无数据区之前的一个有数据的股票ID
    def record_still_have_data_area(self, stockid):
        # logging.info(f"record_still_have_data_area # 24 {stockid}")
        if(0==self.state_none_data_area):
            self.stockid_none_data_area_start = stockid
            # logging.info("record_still_have_data_area # 27 if(0==self.state_none_data_area)")
        else:
            self.stockid_none_data_area_end = stockid

            # # BEGIN 写文件:
            # if(len(self.stockid_none_data_area_start)>4 and len(self.stockid_none_data_area_end)>4):
            #     file_path = os.path.join(folder_path, file_name)
            #     write_line_to_file(file_path, f"{self.stockid_none_data_area_start},{self.stockid_none_data_area_end}")
            # # END 写文件
            
            # logging.info("record_still_have_data_area # 34 else")
            self.stockid_none_data_area_start = stockid
            self.state_none_data_area = 0
        # logging.info("record_still_have_data_area # 35")

    def record_none_data_state(self):
        # logging.info(f"record_none_data_state # 38 {self.stockid_none_data_area_start}")
        self.state_none_data_area = 1
        # logging.info(f"record_none_data_state # 52  stockid_none_data_area_start:{self.stockid_none_data_area_start}")

    # 读取文件,并把文件中的内容放到列表中:
    def read_file(self):
        # logging.info("read_file # 45")
        file_path = os.path.join(folder_path, file_name)
        lines = []
        try:
            with open(file_path, 'r') as file:
                lines = file.readlines()
        except FileNotFoundError:
            # print(f"# 44 文件不存在: {file_path}")
            logging.info("read_file # 53 文件不存在")
        for line in lines:
            line = line.strip()
            # 找出逗号所在的位置
            comma_index = line.find(',')
            self.list_no_data_area.append((line[:comma_index],line[comma_index+1:]))
        logging.info(f"read_file # 58  self.list_no_data_area:{len(self.list_no_data_area)}")
    
    # 判断是无数据区的开始 ,若是, 就跳过区域, 并返回true
    def is_start_of_no_data_area(self, stockid):
        # logging.info( f"is_start_of_no_data_area # 63 stockid:{stockid}" )

        # 跳过所有7开头的股票ID
        if len(stockid)>1 and "7" == stockid[1]:
            end_stockid = "920000"
            # 依次模拟小键盘输入每个数字
            for num in end_stockid:
                # 小键盘数字的按键名称是'num0'到'num9'
                key = f'num{num}'
                pyautogui.press(key)
                # 每个按键之间可以添加短暂延迟，避免输入过快
                time.sleep(0.05)
            pyautogui.press("enter")
            return True

        for start_stockid, end_stockid in self.list_no_data_area:
            if stockid == start_stockid:
                if "完" == end_stockid:
                    logging.info("####    完    ####")
                    raise Exception("####    完    ####")
                # 依次模拟小键盘输入每个数字
                for num in end_stockid:
                    # 小键盘数字的按键名称是'num0'到'num9'
                    key = f'num{num}'
                    pyautogui.press(key)
                    # 每个按键之间可以添加短暂延迟，避免输入过快
                    time.sleep(0.05)
                pyautogui.press("enter")
                time.sleep(0.05)
                # logging.info( "is_start_of_no_data_area #75 return True")
                return True
        # logging.info( "is_start_of_no_data_area #77 return false")
        return False

# 2025-10-10
# 有些有数据的股票, 却被当作无数据
#   看来得在上层调用进行重试.
